﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMTC.Model.Model;
using System.Data.Common;
using System.Data;
using IMPlant.Core.Data.Unity;
using SMTC.Model.TestCore;

namespace SMTC.Dao.Role
{
  public  class RoleQueryDao:BaseDao
    {
        /// <summary>
        /// 获取角色
        /// </summary>
        private const string SQL_SELECT_ROLE = @"SELECT RoleID,RoleName,Description From dbo.SMT_Role WHERE RoleName like @RoleName";
      /// <summary>
      /// 获取角色所对应的成员
      /// </summary>
        private const string SQL_SELECT_MEMBEER_IN_ROLE = @"SELECT UserID,UserName FROM dbo.SMT_UserInfo WHERE UserID 
                                                           IN (SELECT MemberID FROM dbo.SMT_MemberInRole WHERE RoleID=@RoleID)";
      /// <summary>
      /// 选择过滤后的成员
      /// </summary>
        private const string SQL_SELECT_CHOOSE_MEMBEER = @"SELECT UserID,UserName FROM dbo.SMT_UserInfo WHERE UserID 
                                                          NOT IN (SELECT MemberID FROM dbo.SMT_MemberInRole WHERE RoleID=@RoleID)";
      /// <summary>
      /// 检查成员 
      /// </summary>
        private const string SQL_CHECK_MEMBER_IN_ROLE = @"SELECT RoleID,MemberID FROM dbo.SMT_MemberInRole WHERE RoleID=@RoleID AND MemberID=@MemberID";
       /// <summary>
       /// 获取所有权限列表
       /// </summary>
        private const string SQL_SELECT_ALL_FUNCTION = @"SELECT FunctionID,FunctionName FROM dbo.SMT_Function ";
      /// <summary>
      /// 验证角色已有权限
      /// </summary>
        private const string SQL_SELECT_ROLE_FUNCTION = @"SELECT FunctionID FROM dbo.SMT_RoleFunction WHERE RoleID=@RoleID";
      /// <summary>
      /// 根据ID获得角色
      /// </summary>
        private const string SQL_SELECT_ROLE_BY_ID = @"SELECT RoleName,Description FROM dbo.SMT_Role WHERE RoleID=@RoleID";
      
        /// <summary>
        /// 获取ROLELIST
        /// </summary>
        /// <param name="RoleName"></param>
        /// <returns></returns>
        public List<SMT_RoleModel> GetRoleList(string RoleName)
        {
            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@RoleName",RoleName)
             
            };

            return DBTemplete.ExcuteGetList<SMT_RoleModel>(CommandType.Text, SQL_SELECT_ROLE, parameters);
        }
      /// <summary>
      /// 根据ROLEID获取成员
      /// </summary>
      /// <param name="RoleID"></param>
      /// <returns></returns>
        public List<EmpInfo> GetMemberInRoleList(string RoleID)
        {
            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@RoleID",RoleID)
             
            };

            return DBTemplete.ExcuteGetList<EmpInfo>(CommandType.Text, SQL_SELECT_MEMBEER_IN_ROLE, parameters);
        }

        public bool CheckMemberInRole(SMT_MemberInRole_Model model)
        {
            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@RoleID",model.RoleID)
               , DBparameterBuild.Build("@MemberID",model.MemberID)
            };
            SMT_MemberInRole_Model memberinrole = new SMT_MemberInRole_Model();
            memberinrole = DBTemplete.ExcuteGetObject<SMT_MemberInRole_Model>(CommandType.Text, SQL_CHECK_MEMBER_IN_ROLE, parameters);
            if (memberinrole.MemberID != null && memberinrole.RoleID != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

      /// <summary>
      /// 获取权限列表
      /// </summary>
      /// <returns></returns>
        public List<SMT_FunctionModel> GetFunctionList()
        {
            return DBTemplete.ExcuteGetList<SMT_FunctionModel>(CommandType.Text, SQL_SELECT_ALL_FUNCTION);
        }
        public List<SMT_RoleFunctionModel> GetRoleFunctionList(string RoleID)
        {
            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@RoleID",RoleID)
             
            };

            return DBTemplete.ExcuteGetList<SMT_RoleFunctionModel>(CommandType.Text, SQL_SELECT_ROLE_FUNCTION, parameters);
        }
      /// <summary>
      /// 根据ID获取ROLE
      /// </summary>
      /// <param name="RoleID"></param>
      /// <returns></returns>
        public List<SMT_RoleModel> GetRoleByRoleID(string RoleID)
        {
            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@RoleID",RoleID)
             
            };

            return DBTemplete.ExcuteGetList<SMT_RoleModel>(CommandType.Text, SQL_SELECT_ROLE_BY_ID, parameters);
        }
        
       
    }
}
